/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.interfaces;

import goodenglish.domain.model.subt.Subt;
import goodenglish.domain.model.subt.SubtRepository;
import goodenglish.domain.service.ProfWordsBackService;
import goodenglish.domain.service.ServiceException;
import goodenglish.domain.service.SubtBackService;
import goodenglish.domain.service.impl.ProfWordBackServiceFile;
import goodenglish.domain.service.impl.SubtBackServiceFile;
import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.repository.db.SubtRepositoryDb;

import java.io.File;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 程序设置
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class ExportPane extends VBox {

	private static Log logger = LogFactory.getLog(ExportPane.class);
	final SubtChoiceBox subtChoiceBox = new SubtChoiceBox();
	private File rebackSubtFile;
	private File rebackProfWordLibFile;

	public ExportPane() {
		this.setSpacing(Config.VGAP);
		addBack();
		addReBack();

	}

	private void addReBack() {
		this.getChildren().add(new Label("还原及导入功能"));
		addRebackSubt();
		addRebackProfWords();
	}

	public void addBack() {
		this.getChildren().add(new Label("备份及导出功能"));
		this.getChildren().add(subtChoiceBox);
		addExportSentence();
		addBackSubt();
		addBackProfWord();
	}

	private void addBackProfWord() {
		/**
		 * 添加备份我熟悉单词
		 */
		Button backProfWordBtn = new Button("备份熟词");
		backProfWordBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				profWordsBackService.backProfWord();

			}
		});
		this.getChildren().add(backProfWordBtn);
	}

	private void addBackSubt() {
		/**
		 * 添加备份字幕按钮
		 */
		Button backSubtBtn = new Button("备份字幕");
		backSubtBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				Subt subt = subtChoiceBox.getSelectSubt();
				if (null != subt) {
					exportService.backSubt(subt);
					Main.instance.showModalMessage(new MessageDialog("备份字幕成功"));
				}

			}
		});
		this.getChildren().add(backSubtBtn);
	}

	private void addExportSentence() {
		HBox hBox = new HBox(Config.HGAP);

		final TextField filePathText = new TextField("请选择要导出的文件位置");
		filePathText.setMinSize(200, 20);
		filePathText.setEditable(false);
		hBox.getChildren().add(filePathText);

		// 添加选择字幕文件按钮
		Button fileSelectbtn = new Button();
		fileSelectbtn.setText("选择文件");

		fileSelectbtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				File file = new FileChooser().showOpenDialog(null);
				if (null != file) {
					filePathText.setText(file.getAbsolutePath());
				}

			}
		});
		hBox.getChildren().add(fileSelectbtn);

		/**
		 * 添加保存认识单词按钮
		 */
		Button btnSave = new Button("导出句子");
		btnSave.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				// 分析字幕
				logger.info("ui call analyse exist zimu");
				String filename = "";
				if (null == filename || filename.isEmpty()) {
					return;
				}
				Subt subt = subtRepository.getSubtByName(filename);
				subt.getLexicon().exportNewSubtSentesToFile();
			}
		});
		hBox.getChildren().add(btnSave);
		this.getChildren().add(hBox);
	}

	public void addRebackSubt() {

		HBox hBox = new HBox(Config.HGAP);

		hBox.getChildren().add(new Label("选择要字幕"));
		final TextField rebackFilePathText = new TextField("选择要还原的文件文件位置");
		rebackFilePathText.setMinSize(200, 20);
		rebackFilePathText.setEditable(false);
		hBox.getChildren().add(rebackFilePathText);

		// 添加选择字幕文件按钮
		Button fileSelectbtn = new Button();
		fileSelectbtn.setText("选择文件");

		fileSelectbtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				rebackSubtFile = new FileChooser().showOpenDialog(null);
				if (null != rebackSubtFile) {
					rebackFilePathText.setText(rebackSubtFile.getAbsolutePath());
				}

			}
		});
		hBox.getChildren().add(fileSelectbtn);
		/**
		 * 添加还原字幕按钮
		 */
		Button rebackSubtBtn = new Button("还原字幕");
		rebackSubtBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				if (rebackSubtFile != null) {
					try {
						exportService.reBackSubt(rebackSubtFile);
						Main.instance.showModalMessage(new MessageDialog("还原字幕成功"));
					} catch (ServiceException e) {
						Main.instance.showModalMessage(new MessageDialog(e.getMessage()));
					}

				}

			}
		});
		hBox.getChildren().add(rebackSubtBtn);
		this.getChildren().add(hBox);
	}

	public void addRebackProfWords() {
		HBox hBox = new HBox(Config.HGAP);

		hBox.getChildren().add(new Label("还原我熟悉的单词"));
		final TextField rebackFilePathText = new TextField("选择要还原的文件文件位置");
		rebackFilePathText.setMinSize(200, 20);
		rebackFilePathText.setEditable(false);
		hBox.getChildren().add(rebackFilePathText);

		// 添加选择字幕文件按钮
		Button fileSelectbtn = new Button();
		fileSelectbtn.setText("选择文件");

		fileSelectbtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				rebackProfWordLibFile = new FileChooser().showOpenDialog(null);
				if (null != rebackProfWordLibFile) {
					rebackFilePathText.setText(rebackProfWordLibFile.getAbsolutePath());
				}

			}
		});
		hBox.getChildren().add(fileSelectbtn);
		/**
		 * 添加还原字幕按钮
		 */
		Button rebackSubtBtn = new Button("还原我熟悉的单词");
		rebackSubtBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				profWordsBackService.reBackProfWord(rebackProfWordLibFile);

			}
		});
		hBox.getChildren().add(rebackSubtBtn);
		this.getChildren().add(hBox);
	}

	private SubtRepository subtRepository = BeanFactory.getSingleInstance(SubtRepositoryDb.class);
	private SubtBackService exportService = BeanFactory.getSingleInstance(SubtBackServiceFile.class);
	private ProfWordsBackService profWordsBackService = BeanFactory.getSingleInstance(ProfWordBackServiceFile.class);
}
